package com.jiandian.zh.api.auth.impl;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

import org.springframework.stereotype.Component;

import java.util.LinkedList;


/**
 * @author lik
 */
@Slf4j
@Component
public class MyUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    // @Override
    // public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
    //     log.info("第三方登陆" + userId);
    //     return buildUser(userId);
    // }
    //
    // private SocialUserDetails buildUser(String userId) {
    //     // 根据用户名查找用户信息
    //     String password = passwordEncoder.encode("123456");
    //     log.info("数据库密码是" + password);
    //     return new SocialUser(userId,
    //             password,
    //             true,
    //             true,
    //             true,
    //             true,
    //             AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_USER"));
    // }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // log.info("登陆用户名" + username);
        // // todo 目前为了开发方便，打印出了密码，之后去掉
        // String encode = passwordEncoder.encode("123456");
        // System.out.println(encode);
        //
        // SysUser sysUser = sysUserService.selectByUsername(username);
        // if (sysUser == null) {
        //     throw new UsernameNotFoundException("用户不存在");
        // }
        // // 获取用户角色列表
        // // List<SysRole> sysRoles = sysRoleService.selectByUsername(username);
        // // StringBuilder stringBuilder = new StringBuilder();
        // // for (SysRole sysRole : sysRoles) {
        // //     String roleName = sysRole.getRoleName();
        // //     if (StringUtils.isNotBlank(roleName)) {
        // //         stringBuilder.append(roleName).append(",");
        // //     }
        // // }
        // // String permissions = stringBuilder.toString();
        // // if (StringUtils.isNotBlank(permissions)) {
        // //     // 去掉最后一个","
        // //     permissions = permissions.split(",")[0];
        // // }
        //
        // List<SysMenu> sysMenus = sysMenuService.selectByUsername(username);
        // LinkedList<RbacGrantedAuthority> grantedAuthorities = new LinkedList<>();
        // for (SysMenu sysMenu : sysMenus) {
        //     grantedAuthorities.add(new RbacGrantedAuthority(sysMenu.getMethod(),sysMenu.getUrl()));
        // }
        //
        // // LinkedList<RbacGrantedAuthority> rbacGrantedAuthorities = new LinkedList<>();
        // // rbacGrantedAuthorities.add();
        //
        // return new SocialUser(username,
        //         sysUser.getPassword(),
        //         true,
        //         true,
        //         true,
        //         true,
        //         grantedAuthorities );
        LinkedList<GrantedAuthority> grantedAuthorities = new LinkedList<>();
        String encodePassword = passwordEncoder.encode("123456");
        return new User("admin" ,
                encodePassword,
                true,
                true,
                true,
                true,
                grantedAuthorities);
    }
}
